import 'package:flutter/material.dart';
import 'package:kasax/utils/log_utils.dart';

/// 动画示例2
class AnimHomeWidget2 extends StatefulWidget {
  const AnimHomeWidget2({Key? key}) : super(key: key);

  @override
  _AnimHomeWidget2State createState() => _AnimHomeWidget2State();
}

/// 单个动画使用SingleTickerProviderStateMixin,多个动画使用多个的那个
/// 生命周期: forward -> complete
///          reverse -> dismissed
class _AnimHomeWidget2State extends State<AnimHomeWidget2> with SingleTickerProviderStateMixin {
  late AnimationController _animationController;
  double _data = 0.0;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(vsync: this, duration: Duration(seconds: 1));
    _animationController.addListener(() {
      setState(() {
        _data = _animationController.value;
      });
    });
    _animationController.addStatusListener((status) {
      LogUtil.e("$status");
      switch (status) {
        case AnimationStatus.forward:
          LogUtil.e("动画开始执行了");
          break;
        case AnimationStatus.completed:
          // setState(() {
          //   _data = 0.0;
          // });
          LogUtil.e("动画执行完成了");
          break;
        case AnimationStatus.dismissed:
          LogUtil.e("动画取消了");
          break;
        case AnimationStatus.reverse:
          LogUtil.e("动画反转");
          break;
      }
    });
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: ActionChip(
          onPressed: () {
            if (_data <= 0) {
              // 正向执行动画
              _animationController.forward();
            } else {
              // 反转执行
              _animationController.reverse();
            }
          },
          label: Text("$_data"),
        ),
      ),
    );
  }
}
